<?php

namespace Common\Bundle\LibraryBundle\Form;

use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolverInterface;

use Common\Bundle\LibraryBundle\Entity\Repository\ThemeRepository;

class QcmType extends AbstractType
{

    private $idTheme;
    private $em;

    public function __construct($idTheme, $em)
    {
        $this->idTheme = $idTheme;
        $this->em = $em;
    }

    /**
     * @param FormBuilderInterface $builder
     * @param array $options
     */
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $em = $this->em->getManager();
        $idTheme = $this->idTheme;
        $builder
            ->add('nom', 'text', array('attr' => array('class' => 'input-sm'), 'required' => true,))
            ->add('description','textarea', array('attr' => array('class' => 'input-sm'), 'required' => true))
            ->add('theme', 'entity',
                array(
                    'attr' => array('class' => 'input-sm'),
                    'class' => 'Common\Bundle\LibraryBundle\Entity\Theme',
                    'required' => true,
                    'data' => $em->getReference("CommonLibraryBundle:Theme",$idTheme)
                ))
            ->add('niveau', null, array('attr' => array('class' => 'input-sm'), 'required' => true))
            ->add('difficulte', null, array('attr' => array('class' => 'input-sm'), 'required' => true))
            ->add('questions', 'collection', array('type' => new QuestionType(),
                'allow_add'    => true,
                'allow_delete' => true,
                'prototype' => true,
                'prototype_name' => 'questions_name',
                'cascade_validation' => true))
        ;
    }
    
    /**
     * @param OptionsResolverInterface $resolver
     */
    public function setDefaultOptions(OptionsResolverInterface $resolver)
    {
        $resolver->setDefaults(array(
            'data_class' => 'Common\Bundle\LibraryBundle\Entity\Qcm',
            'cascade_validation' => true
        ));
    }

    /**
     * @return string
     */
    public function getName()
    {
        return 'common_bundle_librarybundle_qcm';
    }
}
